package com.fhzn.auth.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.auth.entity.DataResource;
import com.fhzn.auth.entity.User;
import com.fhzn.auth.entity.WebResponse;
import com.fhzn.auth.service.UserService;
import com.fhzn.auth.tools.PageInfoUtils;
import com.fhzn.auth.web.converter.DataResourceConverter;
import com.fhzn.auth.web.converter.UserConverter;
import com.fhzn.auth.web.request.*;
import com.fhzn.auth.web.vo.DataResourceVO;
import com.fhzn.auth.web.vo.UserVO;
import com.fhzn.commons.toolkit.entity.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 老顾
 * @apiNote 用户管理
 * @since 2023/12/27
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/query")
    public WebResponse<PageInfo<UserVO>> query(@ParameterObject UserQueryRequest request) {
        Page<User> userPage = userService.userWithPage(request);
        return WebResponse.success(PageInfoUtils.convert2page(userPage, UserConverter::fromUser));
    }

    @PostMapping("/add")
    public WebResponse<UserVO> add(@RequestBody UserOpRequest request) {
        return WebResponse.success(userService.add(request));
    }

    @PostMapping("/update")
    public WebResponse<String> update(@RequestBody UserOpRequest request) {
        return WebResponse.success(userService.updateUser(request));
    }

    @PostMapping("/addRole")
    public WebResponse<String> addRole(@RequestBody @Validated UserRoleRequest request) {
        return WebResponse.success(userService.addRole(request));
    }

    @GetMapping("/passwordSearch")
    public WebResponse<String> passwordSearch(@ParameterObject UserPasswordRequest request) {
        return WebResponse.success(userService.passwordSearch(request));
    }

    @PostMapping("/resetPassword")
    public WebResponse<String> resetPassword(@RequestBody UserPasswordRequest request) {
        return WebResponse.success(userService.resetPassword(request));
    }

    @PostMapping("/passwordModify")
    public WebResponse<String> passwordModify(@RequestBody UserPasswordRequest request) {
        return WebResponse.success(userService.passwordModify(request));
    }

    @PostMapping("/passwordCheck")
    public WebResponse<String> passwordCheck(@RequestBody UsernamePasswordRequest request) {
        return WebResponse.success(userService.passwordCheck(request));
    }

    @GetMapping("/one")
    public WebResponse<UserVO> queryOneByNickName(String nickname) {
        User user = userService.findUserByNickName(nickname);
        return WebResponse.success(UserConverter.fromUser(user));
    }

    @GetMapping("/getUserDataResByParentCode")
    public WebResponse<List<DataResourceVO>> getUserDataResByParentCode(@ParameterObject DataResourceRequest request) {
        List<DataResource> dataResources = userService.getUserDataResByParentCode(request);
        return WebResponse.success(DataResourceConverter.convertToVoList(dataResources));
    }
}
